<template>
	<view>
		<view class="boxCard">
			<view class="infoItem">
				<text>抬头类型</text>
				<u-radio-group v-model="radioDefaultValue" placement="row">
					<u-radio :customStyle="{marginRight: '20px'}" v-for="(item, index) in radiolist" iconSize="10"
						labelSize="13" activeColor="#009CF9" :key="index" :label="item.name" :name="item.name"
						@change="radioChange(item)">
					</u-radio>
				</u-radio-group>
			</view>

			<view v-if="formData.invoiceTitleType == 1">
				<view class="infoItem">
					<text class="subtitle">公司名称</text>
					<u--input v-model="formData.invoiceTitleName" placeholder="请输入公司名称" border="none" fontSize="13"></u--input>
				</view>
				<view class="infoItem">
					<text class="subtitle">公司税号</text>
					<u--input v-model="formData.dutyParagraph" placeholder="请输入公司税号" border="none" fontSize="13"></u--input>
				</view>
				<view class="infoItem">
					<text>注册地址</text>
					<u--input v-model="formData.registeredAddress" placeholder="请输入注册地址" border="none" fontSize="13"></u--input>
				</view>
				<view class="infoItem">
					<text>注册电话</text>
					<u--input v-model="formData.registeredTelephone" placeholder="请输入注册电话" border="none" fontSize="13"></u--input>
				</view>
				<view class="infoItem">
					<text>开户银行</text>
					<u--input v-model="formData.bank" placeholder="请输入开户银行" border="none" fontSize="13"></u--input>
				</view>
				<view class="infoItem">
					<text>银行账号</text>
					<u--input v-model="formData.bankAccount" placeholder="请输入银行账号" border="none" fontSize="13"></u--input>
				</view>
			</view>

			<view v-if="formData.invoiceTitleType == 2">
				<view class="infoItem">
					<text class="subtitle">抬头名称</text>
					<u--input v-model="formData.invoiceTitleName" placeholder="请输入抬头名称" border="none" fontSize="13" ></u--input>
				</view>
			</view>

		</view>
		<view class="boxCard" v-if="false">
			<view class="setDefaultTitle">
				<view class="tips">
					<text>设为默认抬头</text>
					<text>每次开票会默认填写该抬头信息</text>
				</view>
				<u-switch v-model="formData.isDefaultSwitch" @change="switchChange"></u-switch>
			</view>
            
		</view>
		
		<view class="bottomBtn" @click="saveInvoiceTitle">
			保 存
		</view>
		
	</view>
</template>

<script>
	import {
		setInvoiceTitle,
		invoiceTitleInfo,
		updateInvoiceTitle
	} from '@/api/invoice-api.js'
	export default {
		data() {
			return {
				radiolist: [{
					name: '企业单位',
					code: 1
				}, {
					name: '个人/非企业单位',
					code: 2
				}],
				formData: {
					invoiceTitleType: 1,
					invoiceTitleName: '',
					dutyParagraph: '',
					registeredAddress: '',
					registeredTelephone: '',
					bank: '',
					bankAccount: '',
					isDefaultSwitch:false,
					isDefaultTitle: 2
				},
				type:1,
				invoiceTitleId : ''
			}
		},
		onLoad(op) {
           this.type = op.type;
		   if(this.type == 2){//编辑
		       uni.setNavigationBarTitle({
		       	title: '编辑发票抬头'
		       })
		       this.invoiceTitleId = op.invoiceTitleId;
			   this.getInvoiceTitleInfo();
		   }
		},
		computed: {
		  	radioDefaultValue() {
				if(this.type == 1) {
					return '企业单位'
				}else {
					
					if(this.formData.invoiceTitleType == 1) {
						return '企业单位'
					}else {
						return '个人/非企业单位'
					}
					
				}
			}
		},
		methods: {
			radioChange(item) {
				this.formData.invoiceTitleType = item.code;
			},
			switchChange(value) {
				this.formData.isDefaultSwitch = value;
				this.formData.isDefaultTitle = value == true ? 1 : 2;
			},
			getInvoiceTitleInfo() {
			   invoiceTitleInfo(this.invoiceTitleId).then(res=>{
				   this.formData = res.data;
				   this.formData.isDefaultSwitch = res.data.isDefaultTitle == 1 ? true : false;
			   })	
			},
			
			saveInvoiceTitle() {
				uni.showLoading({
					title: '保存中..'
				})
				if(this.type == 2) {//编辑保存
				   updateInvoiceTitle(this.formData).then(res=> {
					   uni.hideLoading();
					   if(res.code == 200) {
					   	uni.$emit('refreshInvoiceTitleList');
					   	uni.navigateBack({
					   		
					   	})
					   }
				   })
				}else {//新增发票抬头
					setInvoiceTitle(this.formData).then(res=> {
						uni.hideLoading();
						if(res.code == 200) {
							uni.$emit('refreshInvoiceTitleList');
							uni.navigateBack({
								
							})
						}
					})
				}
				
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.boxCard {
		background-color: $bg-white-color;
		border-radius: 16upx;
		margin: 10upx 22upx;
		padding: 30upx 46upx;

	}

	.infoItem {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 20upx 0;

		text {
			font-size: 26upx;
			font-weight: bold;
			color: $text-color-33;
			margin-right: 20upx;
		}
		.subtitle {
			&::after {
				content: '*';
				color: $main-color;
				font-size: 26upx;
				
			}
		}
	}
	.setDefaultTitle{
		display: flex;
		justify-content: space-between;
		.tips {
			display: flex;
			flex-direction: column;
		   text{
			   color: $text-color-33;
			   font-size: 26upx;
			   font-weight: bold;
		   }
		   text:nth-child(2) {
			   color: $text-color-99;
			   font-size: 22upx;
			   margin-top: 16upx;
		   }
		}
		
	}
	.bottomBtn {
			margin: 0 20upx;
			position: fixed;
			bottom: 20upx;
			width: 710upx;
			height: 90upx;
			line-height: 90upx;
			text-align: center;
			color: $text-color-ff;
			font-size: 30upx;
			font-weight: bold;
			background: linear-gradient(to right, #32ace9 0%, #176cfb 100%);
			border-radius: 10upx;
		}
</style>
